	var direction = 0; // Horizontal default
	var writable = false;
	$(document).ready(function() {
	
		$('table.rebus td').contextMenu('myMenu1', {
			bindings: {
				'horizontal': function(t) {
					direction = 0;
				},
				'vertical': function(t) {
					direction = 1;
				},
				'toggle': function(t) {
					$("#" + t.id).toggleClass("empty");
					$("#" + t.id + " input").attr("disabled", !$("#" + t.id + " input").attr("disabled"));
					$("#" + t.id + " input").val($("#" + t.id + " input").attr("disabled")?"0":"");
				}
			}
		});
		
		// the boxes that have value 0 are made black
		$("table.rebus td input[@value='0']").parent().parent().addClass("empty");
	
		$("input[@name^='hint_']").click(function () {
			writable = false;
		});
	
		$('table.rebus td').click(function() {
			writable = true;
			$('table.rebus td.selected').removeClass("selected");
			$(this).addClass("selected");
			var xy = coords($(this).attr("id"));
			$("[@id^='hint_']").removeClass("selectedfield");
			$("#"+getHintFieldId(xy)).addClass("selectedfield");
		});
	
		$(window).keydown(function(event){
			if (writable) {
				if (event.keyCode >= 65 && event.keyCode <= 90) {
					$('table.rebus td.selected:not(.empty) .letter input').val( String.fromCharCode(event.keyCode));
					if (direction == 0) {
						moveRight();
					} else {
						moveDown()
					}
				} else if (event.keyCode == 8 || event.keyCode == 46) { // backspace or delete
					$('table.rebus td.selected:not(.empty) .letter input').val(" ");
				} else if (event.keyCode == 32) {
					$('table.rebus td.selected').toggleClass("empty");
					$("table.rebus td.selected input").attr("disabled", !$("table.rebus td.selected input").attr("disabled"));
				} else if (event.keyCode == 39) { // right arrow
					moveRight();
				} else if (event.keyCode == 38) { // up arrow
					moveUp();
				} else if (event.keyCode == 37) { // left arrow
					moveLeft();
				} else if (event.keyCode == 40) { // down arrow
					moveDown();
				}
			}	
		});	
	});
	
	function moveDown() {
		var xy = coords($('table.rebus td.selected').attr("id"));	
		if (xy["y"] < 9) {
			xy["y"] = xy["y"] * 1 + 1;
			$('#' + cellId(xy)).click();
		}
	}

	function moveLeft() {
		var xy = coords($('table.rebus td.selected').attr("id"));
		if (xy["x"] > 0) {
			xy["x"] = xy["x"] * 1 - 1;
			$('#' + cellId(xy)).click();
		}
	}

	function moveRight() {
		var xy = coords($('table.rebus td.selected').attr("id"));
		if (xy["x"] < 9) {
			xy["x"] = xy["x"] * 1 + 1;
			$('#' + cellId(xy)).click();
		}
	}

	function moveUp() {
		var xy = coords($('table.rebus td.selected').attr("id"));
		if (xy["y"] > 0) {
			xy["y"] = xy["y"] * 1 - 1;
			$('#' + cellId(xy)).click();
		}
	}
	
	function cellId(coords) {
		return "cell_" + coords["x"] + "_" + coords["y"]; 
	}
	
	function coords(id) {
		var x = id.substring(5,6);
		var y = id.substring(7,8);
		return {x:x,y:y};
	}
	
	function getHintFieldId(coords) {
		if (direction == 0) {
			return "hint_h_" + coords["y"];
		} else {
			return "hint_v_" + coords["x"];
		}
	}